<%_ if (hasAdd || hasEdit) { _%>
<%_
// 本表是否有配置 字典数据 的字段
const _hasDict = fieldList.some((fieldItem) => fieldItem.dict != '')
// 本表 配置的字典标识数组（去重过的）
const _uniDictList = [...new Set(fieldList.filter((fieldItem) => fieldItem.dict != '').map((item) => item.dict))]
// 主键字段数据
const pkField =  fieldList.find((item) => item.key === 'PRI') || {}
_%>
<template>
  <el-dialog :title="title" v-model="open" width="1000px" :close-on-click-modal="false" append-to-body>
    <el-form ref="addOrEditFormRef" :model="form" :rules="rules" label-width="auto">
      <%_ for (const field of fieldList) { _%>
      <%_ if (field.addOrEdit) { _%>
      <%_ // 已配置添加编辑显示 _%>
      <el-form-item label="<%= field.label %>" prop="<%= field.name %>">
        <%_ // 判断组件类型 _%>
        <%_ if (field.display === 'input') { _%>
        <%_ // 文本框 _%>
        <%_ if (field.unit) { _%>
        <el-input v-model="form.<%= field.name %>" placeholder="请填写<%= field.label %>">
          <template #append><%= field.unit %></template>
        </el-input>
        <%_ } else { _%>
        <el-input v-model="form.<%= field.name %>" placeholder="请填写<%= field.label %>"></el-input>
        <%_ } _%>
        <%_ } else if (field.display === 'inputNumber') { _%>
        <%_ // 数字框 _%>
        <%_ if (field.unit) { _%>
        <el-input v-model="form.<%= field.name %>" placeholder="请填写<%= field.label %>" type="number">
          <template #append><%= field.unit %></template>
        </el-input>
        <%_ } else { _%>
        <el-input v-model="form.<%= field.name %>" placeholder="请填写<%= field.label %>" type="number"></el-input>
        <%_ } _%>
        <%_ } else if (field.display === 'textarea') { _%>
        <%_ // 文本域 _%>
        <el-input v-model="form.<%= field.name %>" placeholder="请填写<%= field.label %>" type="textarea"></el-input>
        <%_ } else if (field.display === 'select') { _%>
        <%_ // 下拉框 _%>
        <el-select v-model="form.<%= field.name %>" placeholder="请选择<%= field.label %>" clearable>
          <%_ if (field.dict) { _%>
          <el-option v-for="dict in <%= field.dict %>" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
          <%_ } else { _%>
          <el-option label="选择字典生成或手动编写数据" value="1"></el-option>
          <%_ } _%>
        </el-select>
        <%_ } else if (field.display === 'radio') { _%>
        <%_ // 单选框 _%>
        <el-radio-group v-model="form.<%= field.name %>">
          <%_ if (field.dict) { _%>
          <el-radio v-for="dict in <%= field.dict %>" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
          <%_ } else { _%>
          <el-radio label="1">选择字典生成或手动编写数据</el-radio>
          <%_ } _%>
        </el-radio-group>
        <%_ } else if (field.display === 'checkbox') { _%>
        <%_ // 复选框 _%>
        <el-checkbox-group v-model="form.<%= field.name %>">
          <%_ if (field.dict) { _%>
          <el-checkbox v-for="dict in <%= field.dict %>" :key="dict.value" :label="dict.value">{{ dict.label }}</el-checkbox>
          <%_ } else { _%>
          <el-checkbox label="1">选择字典生成或手动编写数据</el-checkbox>
          <%_ } _%>
        </el-checkbox-group>
        <%_ } else if (field.display === 'date') { _%>
        <%_ // 日期 _%>
        <el-date-picker v-model="form.<%= field.name %>" type="date" value-format="YYYY-MM-DD" placeholder="请选择<%= field.label %>"></el-date-picker>
        <%_ } else if (field.display === 'datetime') { _%>
        <%_ // 日期时间 _%>
        <el-date-picker v-model="form.<%= field.name %>" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择<%= field.label %>"></el-date-picker>
        <%_ } else if (field.display === 'imageUpload') { _%>
        <%_ // 图片上传 _%>
        <image-upload v-model="form.<%= field.name %>" :limit="1"></image-upload>
        <%_ } else if (field.display === 'fileUpload') { _%>
        <%_ // 文件上传 _%>
        <file-upload v-model="form.<%= field.name %>" :limit="1"></file-upload>
        <%_ } else if (field.display === 'editor') { _%>
        <%_ // 富文本 _%>
        <editor v-model="form.<%= field.name %>" :min-height="200"></editor>
        <%_ } else { %>
        <%_ // 其他 _%>
        <%_ } _%>
      </el-form-item>
      <%_ } _%>
      <%_ } _%>
      <%_ if (subTable) { _%>
      <!-- 子表编辑 -->
      <el-form-item label="<%= _sub.comment %>" prop="subTableData">
        <el-table :data="form.subTableData" empty-text="暂无数据，点击下方+号填写信息~">
          <%_ for (const subField of _sub.fieldList) { _%>
          <%_ if (subField.addOrEdit) { _%>
          <%_ const label = subField.unit ? `${subField.label}（${subField.unit}）` : subField.label _%>
          <el-table-column label="<%= label %>" align="center" prop="<%= subField.name %>">
            <%_ if (subField.required) { _%>
            <template #header> <span class="roc-require"></span><%= subTableLabel %> </template>
            <%_ } _%>
            <template #default="{ row }">
              <%_ if (subField.display === 'input') { _%>
              <%_ // 文本框 _%>
              <el-input v-model="row.<%= subField.name %>" placeholder="请填写<%= subField.label %>"></el-input>
              <%_ } else if (subField.display === 'inputNumber') { _%>
              <%_ // 数字框 _%>
              <el-input v-model="row.<%= subField.name %>" placeholder="请填写<%= subField.label %>" type="number"></el-input>
              <%_ } else if (subField.display === 'textarea') { _%>
              <%_ // 文本域 _%>
              <el-input v-model="row.<%= subField.name %>" placeholder="请填写<%= subField.label %>" type="textarea"></el-input>
              <%_ } else if (subField.display === 'select') { _%>
              <%_ // 下拉框 _%>
              <el-select v-model="row.<%= subField.name %>" placeholder="请选择<%= subField.label %>" clearable>
                <%_ if (subField.dict) { _%>
                <el-option v-for="dict in <%= subField.dict %>" :key="dict.value" :label="dict.label" :value="dict.value"></el-option>
                <%_ } else { _%>
                <el-option label="选择字典生成或手动编写数据" value="1"></el-option>
                <%_ } _%>
              </el-select>
              <%_ } else if (subField.display === 'radio') { _%>
              <%_ // 单选框 _%>
              <el-radio-group v-model="row.<%= subField.name %>">
                <%_ if (subField.dict) { _%>
                <el-radio v-for="dict in <%= subField.dict %>" :key="dict.value" :label="dict.value">{{ dict.label }}</el-radio>
                <%_ } else { _%>
                <el-radio label="1">选择字典生成或手动编写数据</el-radio>
                <%_ } _%>
              </el-radio-group>
              <%_ } else if (subField.display === 'checkbox') { _%>
              <%_ // 复选框 _%>
              <el-checkbox-group v-model="row.<%= subField.name %>">
                <%_ if (subField.dict) { _%>
                <el-checkbox v-for="dict in <%= subField.dict %>" :key="dict.value" :label="dict.value">{{ dict.label }}</el-checkbox>
                <%_ } else { _%>
                <el-checkbox label="1">选择字典生成或手动编写数据</el-checkbox>
                <%_ } _%>
              </el-checkbox-group>
              <%_ } else if (subField.display === 'date') { _%>
              <%_ // 日期 _%>
              <el-date-picker v-model="row.<%= subField.name %>" type="date" value-format="YYYY-MM-DD" placeholder="请选择<%= subField.label %>"></el-date-picker>
              <%_ } else if (subField.display === 'datetime') { _%>
              <%_ // 日期时间 _%>
              <el-date-picker v-model="row.<%= subField.name %>" type="datetime" value-format="YYYY-MM-DD HH:mm:ss" placeholder="请选择<%= subField.label %>"></el-date-picker>
              <%_ } else if (subField.display === 'imageUpload') { _%>
              <%_ // 图片上传 _%>
              <image-upload v-model="row.<%= subField.name %>" :limit="1"></image-upload>
              <%_ } else if (subField.display === 'fileUpload') { _%>
              <%_ // 文件上传 _%>
              <file-upload v-model="row.<%= subField.name %>" :limit="1"></file-upload>
              <%_ } else if (subField.display === 'editor') { _%>
              <%_ // 富文本 _%>
              <editor v-model="row.<%= subField.name %>" :min-height="200"></editor>
              <%_ } else { %>
              <%_ // 其他 _%>
              <%_ } _%>
            </template>
          </el-table-column>
          <%_ } _%>
          <%_ } _%>
          <el-table-column label="操作" align="center" width="150">
            <template #default="{ $index }">
              <el-button link type="primary" icon="Delete" @click="handleSubTableDelete($index)" v-autoBlur> 删除 </el-button>
            </template>
        </el-table-column>
        </el-table>
        <el-icon @click="handleSubTableAdd" size="40" color="#909399" style="margin: 0 auto; cursor: pointer; margin-top: 10px"><CirclePlus /></el-icon>
      </el-form-item>
      <%_ } _%>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose" v-autoBlur>取 消</el-button>
        <el-button type="primary" @click="submitForm" v-autoBlur>确 定</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup name="<%= fn.snakeFormatHump(name, true) %>AddOrEdit">
import { ref, getCurrentInstance, inject } from 'vue'
import { cloneDeep } from 'lodash-es'
<%_ if (hasAdd && hasEdit) { _%>
import { add<%= fn.snakeFormatHump(name, true) %>, edit<%= fn.snakeFormatHump(name, true) %> } from '@/api/<%= fn.snakeFormatHump(name) %>'
<%_ } else if (hasAdd === true && hasEdit === false) { _%>
import { add<%= fn.snakeFormatHump(name, true) %> } from '@/api/<%= fn.snakeFormatHump(name) %>'
<%_ } else if (hasAdd === false && hasEdit === true) { _%>
import { edit<%= fn.snakeFormatHump(name, true) %> } from '@/api/<%= fn.snakeFormatHump(name) %>'
<%_ } _%>

const { proxy } = getCurrentInstance()
const emit = defineEmits(['updateList'])
<%_ if (_hasDict) { _%>
// 注入字典数据
<%_ for (const dict of _uniDictList) { _%>
const <%= dict %> = inject('<%= dict %>', () => [])
<%_ } _%>
<%_ } _%>

const form = ref({})
const rules = ref({
<%_ for (const field of fieldList) { _%>
  <%_ if (field.addOrEdit) { _%>
  <%_ if (field.required) { _%>
  <%= field.name %>: [{ required: true, message: '<%= field.label %>不能为空', trigger: 'blur' }],
  <%_ } _%>
  <%_ } _%>
<%_ } _%>
})
/**
 * 提交表单
 */
function submitForm() {
  proxy.$refs['addOrEditFormRef'].validate((valid) => {
    if (valid) {
      const _form = cloneDeep(form.value)
      <%_ if (hasAdd && hasEdit) { _%>
      if (!_form.<%= pkField.name %>) {
        add<%= fn.snakeFormatHump(name, true) %>(_form).then(() => {
          proxy.$modal.msgSuccess('添加成功')
          open.value = false
          emit('updateList')
        })
      } else {
        edit<%= fn.snakeFormatHump(name, true) %>(_form).then(() => {
          proxy.$modal.msgSuccess('修改成功')
          open.value = false
          emit('updateList')
        })
      }
      <%_ } else if (hasAdd === true && hasEdit === false) { _%>
      add<%= fn.snakeFormatHump(name, true) %>(_form).then(() => {
        proxy.$modal.msgSuccess('添加成功')
        open.value = false
        emit('updateList')
      })
      <%_ } else if (hasAdd === false && hasEdit === true) { _%>
      edit<%= fn.snakeFormatHump(name, true) %>(_form).then(() => {
        proxy.$modal.msgSuccess('修改成功')
        open.value = false
        emit('updateList')
      })
      <%_ } _%>
    }
  })
}

/**
 * 重置表单
 */
function reset() {
  form.value = {
  <%_ for (const field of fieldList) { _%>
    <%_ if (field.addOrEdit) { _%>
    <%_ if (field.display === 'checkbox') { _%>
    <%= field.name %>: [],
    <%_ } else { _%>
    <%= field.name %>: null,
    <%_ } _%>
    <%_ } _%>
  <%_ } _%>
  <%_ // 是否有子表数据 _%>
  <%_ if (subTable) { _%>
    subTableData: [],
  <%_ } _%>
  }
  proxy.resetForm('addOrEditFormRef')
}

const open = ref(false)
const title = ref('')
<%_ if (hasAdd) { _%>
/**
 * 打开添加对话框
 */
function handleAdd() {
  reset()
  open.value = true
  title.value = '添加'
}
<%_ } _%>
<%_ if (hasEdit) { _%>
/**
 * 打开编辑对话框
 * @param {*} row
 */
function handleEdit(row) {
  reset()
  form.value = row
  open.value = true
  title.value = '修改'
}
<%_ } _%>

/**
 * 关闭对话框
 */
function handleClose() {
  open.value = false
}
<%_ if (subTable) { _%>

<%_ // 子表一行数据 _%>
const subTableDataItem = {
  <%_ for (const subField of _sub.fieldList) { _%>
  <%_ if (subField.addOrEdit) { _%>
  <%_ if(subField.display === 'checkbox') { _%>
  <%= subField.name %>: [],
  <%_ } else { _%>
  <%= subField.name %>: null,
  <%_ } _%>
  <%_ } _%>
  <%_ } _%>
}

/**
 * 添加子表数据
 */
function handleSubTableAdd() {
  form.value.subTableData.push(cloneDeep(subTableDataItem))
}

/**
 * 删除子表数据
 * @param {Number} index
 */
function handleSubTableDelete(index) {
  form.value.subTableData.splice(index, 1)
}
<%_ } _%>

<%_ if (hasAdd && hasEdit) { _%>
defineExpose({ handleAdd, handleEdit })
<%_ } else if (hasAdd === true && hasEdit === false) { _%>
defineExpose({ handleAdd })
<%_ } else if (hasAdd === false && hasEdit === true) { _%>
defineExpose({ handleEdit })
<%_ } _%>
</script>

<style lang="scss" scoped></style>
<%_ } else { _%>
<template>
  <h2>未生成添加编辑功能，请手动删除本文件</h2>
</template>
<%_ } _%>
